//
//  SCNetworkTool.m
//  Mutal
//
//  Created by TonyStart on 16/10/18.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCNetworkTool.h"

#import "SCAlertView.h"

@implementation SCNetworkTool
+(instancetype)shared{
    static SCNetworkTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[SCNetworkTool alloc] initWithBaseURL:nil];
    });
    return instance;
}

NSString * urlMakeWithDict(NSDictionary *dict) {
    NSMutableString *requestUrl = [NSMutableString stringWithString:BASE_URL];
    NSMutableArray *paramArray = [NSMutableArray array];
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSString *paramStr = [NSString stringWithFormat:@"%@=%@",key,obj];
        [paramArray addObject:paramStr];
    }];
    for (int i=0; i<paramArray.count; i++) {
        NSString *paramStr = nil;
        if (i<paramArray.count - 1) {
            paramStr = [NSString stringWithFormat:@"%@&",paramArray[i]];
        }else{
            paramStr = paramArray[i];
        }
       
        [requestUrl appendString:paramStr];
    }
    return requestUrl;
}

NSString * urlAndParamBuild(NSString *g,NSString *m,NSString *a){
    NSMutableArray *arrp = [NSMutableArray array];
    if (g && g.length) {
        [arrp addObject:[NSString stringWithFormat:@"g=%@&",g]];
    }
    if (m && m.length) {
        [arrp addObject:[NSString stringWithFormat:@"m=%@&",m]];
    }
    if (a && a.length) {
        [arrp addObject:[NSString stringWithFormat:@"a=%@",a]];
    }
    NSMutableString *requestUrl = [NSMutableString stringWithString:BASE_URL];
    
    if (arrp.count) {
        for (NSString *str in arrp) {
            [requestUrl appendString:str];
        }
    }
    return requestUrl;
}
-(void)requestDataWithMethod:(requetMethod)method url:(NSString*)url finish:(void(^)(id data,NSError *err,NSInteger code))finish{
    
    //检查网络连接是否通畅
    AFNetworkReachabilityManager *reachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [reachabilityManager startMonitoring];
    [reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusNotReachable) {
            //网络无连接的提示
            [SCAlertView alertViewWithTitle:@"当前没有网络，请检测网络连接"];
          
            return;
        }
    }];
    
   //设置网络请求超时时长 20秒后取消
    [self.requestSerializer setTimeoutInterval:20];
    if (method == GET) {
    
        [self GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSInteger codeStatus = [responseObject[@"code"] integerValue];
           
            if (codeStatus != 200) {
                [SCAlertView alertViewWithTitle:responseObject[@"msg"]];
                return;
            }
            id data = responseObject[@"list"];
            if (data) {
                finish(data,nil,codeStatus);
            }else{
                id msg = responseObject[@"msg"];
                finish(msg,nil,codeStatus);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finish(nil,error,-1);
        }];
    }else{
        [self POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSInteger codeStatus = [responseObject[@"code"] integerValue];
            finish(responseObject,nil,codeStatus);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finish(nil,error,-1);
        }];
    }
}
@end
